package com.alex.xmlyfm.entities;

import com.alex.xmlyfm.Constants;
import com.alex.xmlyfm.entities.albumdetail.AlbumDetailHeader;
import com.alex.xmlyfm.entities.albumdetail.AlbumDetailTrack;
import com.alex.xmlyfm.entities.discoverrecommend.DiscoverRecommendAlbums;
import com.alex.xmlyfm.entities.discoverrecommend.DiscoverRecommendColumns;
import com.alex.xmlyfm.entities.discoverrecommend.DiscoverRecommendItem;
import com.alex.xmlyfm.entities.discoverrecommend.DiscoverRecommendSpecial;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by
 * Author: alex
 * Email:
 * Date: 15-10-20.
 */
public class EntityParseUtil {
    private EntityParseUtil(){

    }

    public static List<DiscoverCategory> parseDiscoverCategories(JSONObject jsonObject) {
        List<DiscoverCategory> ret = null;
        if (jsonObject != null) {
            try {
                int code = jsonObject.getInt("ret");

                // 获取数据成功
                if (code == Constants.TASK_RESULT_OK) {
                    ret = new LinkedList<>();
                    JSONArray array = jsonObject.getJSONArray("list");
                    int len = array.length();
                    if (len > 0) {
                        for (int i = 0; i < len; i++) {
                            JSONObject object = array.getJSONObject(i);
                            DiscoverCategory category = new DiscoverCategory();
                            // 利用实体类内部实现 JSON 解析，外部的代码调用方法即可
                            category.parseJSON(object);
                            ret.add(category);
                        }
                    }

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return ret;
    }

    /**
     * 解析专辑详情的头部
     * @param jsonObject
     * @return
     */
    public static AlbumDetailHeader parseAlbumDetailHeader(JSONObject jsonObject){
        AlbumDetailHeader ret = null;
        if (jsonObject != null){
            try {
                int code = jsonObject.getInt("ret");
                if (code == Constants.TASK_RESULT_OK){
                    ret = new AlbumDetailHeader();
                    JSONObject object = jsonObject.getJSONObject("album");
                    ret.parseJSON(object);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return ret;
    }

    /**
     * 解析专辑详情的曲目
     * @param jsonObject
     * @return
     */
    public static List<AlbumDetailTrack> parseAlbumDetailTrack(JSONObject jsonObject){
        List<AlbumDetailTrack> ret = null;
        if (jsonObject != null){
            try {
                int code = jsonObject.getInt("ret");
                if (code == Constants.TASK_RESULT_OK){
                    // TODO
                    ret = new LinkedList<>();
                    JSONObject jsonTracks = jsonObject.getJSONObject("tracks");
                    JSONArray array = jsonTracks.getJSONArray("list");
                    int len = array.length();
                    for (int i = 0; i < len; i++) {
                        JSONObject obj = array.getJSONObject(i);
                        AlbumDetailTrack track = new AlbumDetailTrack();
                        track.parseJSON(obj);
                        ret.add(track);
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        return ret;

    }

    /**
     * 解析发现 模块中 推荐栏目中的数据结构
     * @param jsonObject
     * @return
     */
    public static List<DiscoverRecommendItem> parseDiscoverRecommends(JSONObject jsonObject) {
        List<DiscoverRecommendItem> ret = null;

        if (jsonObject != null) {
            try {
                int code = jsonObject.getInt("ret");
                if (code == Constants.TASK_RESULT_OK){
                    ret = new LinkedList<>();
                    // 小编推荐内容的解析
                    JSONObject object = jsonObject.getJSONObject("editorRecommendAlbums");
                    DiscoverRecommendAlbums editorRecommend = new DiscoverRecommendAlbums();

                    editorRecommend.parseJSON(object);
                    ret.add(editorRecommend);


                    // 解析精品听单
                    JSONObject specialColumn = jsonObject.getJSONObject("specialColumn");
                    DiscoverRecommendSpecial special = new DiscoverRecommendSpecial();
                    special.parseJSON(specialColumn);
                    ret.add(special);

                    // 解析发现新奇

                    JSONObject disNewJSON = jsonObject.getJSONObject("discoveryColumns");
                    DiscoverRecommendColumns discoverColumns = new DiscoverRecommendColumns();
                    discoverColumns.parseJSON(disNewJSON);
                    ret.add(discoverColumns);

                    // 解析热门推荐
                    JSONObject hotRecommendJSON = jsonObject.getJSONObject("hotRecommends");
                    JSONArray hotList = hotRecommendJSON.getJSONArray("list");
                    int len = hotList.length();
                    for (int i = 0; i < len; i++) {
                        // 获取热门推荐内部 List 中的每一个推荐信息
                        JSONObject discoverRecommendAlbumJSON = hotList.getJSONObject(i);
                        DiscoverRecommendAlbums album = new DiscoverRecommendAlbums();
                        album.parseJSON(discoverRecommendAlbumJSON);
                        ret.add(album);
                    }
                    
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
        return ret;
    }


    public static List<String> parseFocusImages(JSONObject jsonObject) throws JSONException {
        List<String> ret = new LinkedList<>();

        JSONObject object = jsonObject.getJSONObject("focusImages");
        JSONArray array = object.getJSONArray("list");
        for (int i = 0; i < array.length(); i++) {
            JSONObject picJSON = array.getJSONObject(i);
            String picUrl = picJSON.getString("pic");
            ret.add(picUrl);
        }

        return ret;
    }





}